<?php

namespace App\Models\Company;

use App\Facades\Site;
use Illuminate\Foundation\Auth\User as Authenticatable;

class CompanyUser extends Authenticatable{
    //设置表的类型和表名
    protected $table = 'company_users';
    protected $primaryKey = 'uid';
    public $timestamps = false;
    protected $guarded = [];


    /**
     * 获取积分数
     * @param $value
     */
    public function integralNumber(){
        if($this->viptime > date('Y-m-d H:i:s')){
            return $this->vip_integral + $this->integral;
        }else{
            return $this->integral;
        }
    }

    /**
     * 扣除积分数
     * @param $value
     */
    public function integralDeduct($value='0'){
        if($this->viptime > date('Y-m-d H:i:s')){
            if($this->vip_integral!=0 && $this->vip_integral >= $value){
                $this->vip_integral = $this->vip_integral - $value;
                $this->save();
                return true;
            }else{
                $total = $this->vip_integral + $this->integral;
                if($total!=0 && $total >= $value){
                    $this->integral = $this->integral - ($value - $this->vip_integral);
                    $this->vip_integral = 0;
                    $this->save();
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            if($this->integral!=0 && $this->integral >= $value){
                $this->integral = $this->integral - $value;
                $this->save();
                return true;
            }else{
                return false;
            }
        }
    }


    /**
     * 获取简历数
     * @param $value
     */
    public function resumeNumber(){
        if($this->viptime > date('Y-m-d H:i:s')){
            return $this->vip_resume_number + $this->resume_number;
        }else{
            return $this->resume_number;
        }
    }

    /**
     * 扣除简历数
     * @param $value
     */
    public function resumeDeduct($value='0'){
        if($this->viptime > date('Y-m-d H:i:s')){
            if($this->vip_resume_number!=0 && $this->vip_resume_number >= $value){
                $this->vip_resume_number = $this->vip_resume_number - $value;
                $this->save();
                return true;
            }else{
                $total = $this->vip_resume_number + $this->resume_number;
                if($total!=0 && $total >= $value){
                    $this->resume_number = $this->resume_number - ($value - $this->vip_resume_number);
                    $this->vip_resume_number = 0;
                    $this->save();
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            if($this->resume_number!=0 && $this->resume_number >= $value){
                $this->resume_number = $this->resume_number - $value;
                $this->save();
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 获取职位数
     * @param $value
     */
    public function postNumber(){
        if($this->viptime > date('Y-m-d H:i:s')){
            return $this->vip_post_number;
        }else{
            $jobCount=CompanyJob::where('uid',$this->uid)->where('job_status','!=',4)->count();
            return Site::get('notVipJobNumLimit')-$jobCount;
        }
    }

    /**
     * 扣除职位数
     * @param $value
     */
    public function postDeduct($value='0'){
        if($this->viptime > date('Y-m-d H:i:s')){
            $this->vip_post_number = $this->vip_post_number - $value;
            $this->save();
            return true;
        }
    }

    /**
     * 关联客服信息
     */
    public function withService(){
        return $this->hasOne("App\Models\Crm\CrmUser","uid","crm_uid");
    }
//    /**
//     * 返回积分总数
//     * @param $value
//     */
//    public function setIntegralNumberAttribute($value){
//        list($type, $val) = explode('_', $value);
//        switch($type){
//            case 'less' :
//                if($this->viptime > date('Y-m-d H:i:s')){
//                    $this->attributes['integral_number'] = $this->vip_integral + $this->integral - $val;
//                }else{
//                    $this->attributes['integral_number'] = $this->integral - $val;
//                }
//                break;
//            case 'add' :
//                if($this->viptime > date('Y-m-d H:i:s')){
//                    $this->attributes['integral_number'] = $this->vip_integral + $this->integral + $val;
//                }else{
//                    $this->attributes['integral_number'] = $this->integral + $val;
//                }
//                break;
//            case 'all' :
//                if($this->viptime > date('Y-m-d H:i:s')){
//                    $this->attributes['integral_number'] = $this->vip_integral + $this->integral;
//                }else{
//                    $this->attributes['integral_number'] = $this->integral;
//                }
//                break;
//        }
//    }
}